#!/usr/bin/env python
# coding=utf-8
#--------------------------------------------------------------------------
# 文件名:       auto_ln.py 
# 说明：        source 为源码目录，默认为 /usr/share/pyshared/openerp
#				dist 为编译文件目录，默认为 /usr/lib/pymodules/python2.6/openerp
#
# 版本：        0.01
# 作者：        heyun51 <heyun51@gmail.com>
#
# 创建时间：    2012-06-12 
# 许可证：      Apache License V2.0
#--------------------------------------------------------------------------

import os

def auto_ln(source, dist, filters=[]):
    '''
    递归批量处理软链
    '''
    for root, dirs, files in os.walk(os.path.join(source)):

        if filter(root, filters):
            continue

        for dirName in dirs:

            cwd_dir = os.path.join(dist, root[len(os.path.join(source))+1:], dirName)
            if not os.path.exists(cwd_dir) and not filter(dirName, filters):
                os.makedirs(cwd_dir)
                print(cwd_dir)

        for fileName in files:

            cwd_file = os.path.join(dist, root[len(os.path.join(source))+1:], fileName)
            if not os.path.islink(cwd_file) and not os.path.isfile(cwd_file) and not filter(fileName, filters):
                os.system("ln -s %s %s" % (os.path.join(root, fileName), cwd_file))
                print(os.path.join(root, fileName) + " -> " + cwd_file)

#------------------------------------------ End def auto_ln()

def filter(root, filters=[]):

    for fd in filters:
        if fd in root:
            return True

    return False

#------------------------------------------ End def filter()

if __name__ == "__main__":
    filters = [".swp", ".swo", ".git"]

    cwd = os.getcwd().split("/")[-1:][0]
    source = os.path.join(os.getcwd(), cwd)
    dist = os.path.join("/usr/lib/pymodules/python2.7/openerp/addons/", cwd)

    if not os.path.exists(dist):
        os.makedirs(dist)

    auto_ln(source, dist, filters)
